<?php
/**
 * OWL Default Widget
 *
 * @package OWL
 * @subpackage Widget
 * @since 1.0
 * @author trung, KeiMOon
 */

class Owl_Interest_Widgets extends Owl_Widgets {
    function Owl_Interest_Widgets(){
        $this->Owl_Widgets('interest', __('Interests'), __('Display your interests'));
    }

    function widget($instance){
        $number = !empty($instance['number']) ? $instance['number'] : 5;
        $me = new Owl_Personal();
        $info = $me->get_personal();
        if (is_array($info["interests"])) {
            $interests = array_slice($info["interests"], 0, $number);
            $link = Owl_Link::template_url("info", false) . "#interests";
            $to_print = array();
            foreach ($interests as $interest) {
                $to_print[] = Owl_Html::make_it_link($link, Owl_Format::esc_html(__($interest, true)));
            }
            echo Owl_Html::make_it_list($to_print);
        }
        else {
            echo __("No information");
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        $instance['number'] = $new_instance['number'];
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Interests'), 'number' => 5 ));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
<p>
    <label for="<?php echo $this->get_field_id('number'); ?>"><?php echo __('Number'); ?></label>
    <select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat">
        <option value="5" <?php Owl_Html::selected( $instance['number'], '5' ); ?>><?php echo('5'); ?></option>
        <option value="10" <?php Owl_Html::selected( $instance['number'], '10' ); ?>><?php echo('10'); ?></option>
    </select>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Interest_Widgets);

class Owl_Academic_Widgets extends Owl_Widgets {
    function Owl_Academic_Widgets(){
        $this->Owl_Widgets('academic', __('Academics record'), __('Display your academics record'));
    }

    function widget($instance){
        $number = !empty($instance['number']) ? $instance['number'] : 5;
        $me = new Owl_Personal();
        $info = $me->get_personal();
        if (is_array($info["academics"])) {
            $academics = array_slice($info["academics"], 0, $number);
            $link = Owl_Link::template_url("info", false) . "#academics";
            $to_print = array();
            foreach ($academics as $academic) {
                $to_print[] = Owl_Html::make_it_link($link, Owl_Format::esc_html(__($academic, true)));
            }
            echo Owl_Html::make_it_list($to_print);
        }
        else {
            echo __("No information");
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        $instance['number'] = $new_instance['number'];
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Academics record'), 'number' => 5 ));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
<p>
    <label for="<?php echo $this->get_field_id('number'); ?>"><?php echo __('Number'); ?></label>
    <select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat">
        <option value="5" <?php Owl_Html::selected( $instance['number'], '5' ); ?>><?php echo('5'); ?></option>
        <option value="10" <?php Owl_Html::selected( $instance['number'], '10' ); ?>><?php echo('10'); ?></option>
    </select>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Academic_Widgets);

class Owl_Certification_Widgets extends Owl_Widgets {
    function Owl_Certification_Widgets(){
        $this->Owl_Widgets('certification', __('Certifications'), __('Display your certifications'));
    }

    function widget($instance){
        $number = !empty($instance['number']) ? $instance['number'] : 5;
        $me = new Owl_Personal();
        $info = $me->get_personal();
        if (is_array($info["certifications"])) {
            $certifications = array_slice($info["certifications"], 0, $number);
            $link = Owl_Link::template_url("info", false) . "#certifications";
            $to_print = array();
            foreach ($certifications as $certification) {
                $to_print[] = Owl_Html::make_it_link($link, Owl_Format::esc_html(__($certification, true)));
            }
            echo Owl_Html::make_it_list($to_print);
        }
        else {
            echo __("No information");
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        $instance['number'] = $new_instance['number'];
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Certifications'), 'number' => 5 ));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
<p>
    <label for="<?php echo $this->get_field_id('number'); ?>"><?php echo __('Number'); ?></label>
    <select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat">
        <option value="5" <?php Owl_Html::selected( $instance['number'], '5' ); ?>><?php echo('5'); ?></option>
        <option value="10" <?php Owl_Html::selected( $instance['number'], '10' ); ?>><?php echo('10'); ?></option>
    </select>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Certification_Widgets);

class Owl_Course_Widgets extends Owl_Widgets {
    function Owl_Course_Widgets(){
        $this->Owl_Widgets('course', __('Courses'), __('Display your courses'));
    }

    function widget($instance){
        $number = !empty($instance['number']) ? $instance['number'] : 5;
        $owl_course = new Owl_Course();
        $all_courses = $owl_course->get_all_courses();
        if (!empty($all_courses)) {
            $courses = array_slice($all_courses, 0, $number);
            $link = Owl_Link::template_url("course", false);
            $to_print = array();
            foreach ($courses as $course) {
                $to_print[] = Owl_Html::make_it_link($link, Owl_Format::esc_html(__($course["name"], true)));
            }
            echo Owl_Html::make_it_list($to_print);
        }
        else {
            echo __("No information");
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        $instance['number'] = $new_instance['number'];
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Courses'), 'number' => 5 ));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
<p>
    <label for="<?php echo $this->get_field_id('number'); ?>"><?php echo __('Number'); ?></label>
    <select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat">
        <option value="5" <?php Owl_Html::selected( $instance['number'], '5' ); ?>><?php echo('5'); ?></option>
        <option value="10" <?php Owl_Html::selected( $instance['number'], '10' ); ?>><?php echo('10'); ?></option>
    </select>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Course_Widgets);

class Owl_Document_Widgets extends Owl_Widgets {
    function Owl_Document_Widgets(){
        $this->Owl_Widgets('document', __('Documents'), __('Display your documents'));
    }

    function widget($instance){
        $number = !empty($instance['number']) ? $instance['number'] : 5;
        $owl_course = new Owl_Course();
        if ($instance["course"] == '0') {
            $docs = $owl_course->get_all_documents($number);
            if (!empty ($docs)) {
                $to_print = array();
                foreach ($docs as $doc) {
                    $to_print[] = Owl_Html::make_it_link(Owl_Link::site_url(false) . $doc["link"], Owl_Format::decode_xml($doc["name"]));
                }
                echo Owl_Html::make_it_list($to_print);
            }
            else {
                echo __("No information");
            }
        }
        else {
            $course = $owl_course->get_course(trim($instance["course"]));
            if (is_array($course["documents"])) {
                $to_print = array();
                foreach ($course["documents"] as $doc) {
                    $to_print[] = Owl_Html::make_it_link(Owl_Link::site_url(false) . $doc["link"], Owl_Format::esc_html($doc["name"]));
                }
                echo Owl_Html::make_it_list($to_print);
            }
            else {
                echo __("No information");
            }
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        $instance['number'] = $new_instance['number'];
        $instance['course'] = $new_instance['course'];
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Documents'), 'number' => 5, 'course' => '0' ));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
<p>
    <label for="<?php echo $this->get_field_id('number'); ?>"><?php echo __('Number'); ?></label>
    <select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat">
        <option value="5"<?php Owl_Html::selected( $instance['number'], '5' ); ?>><?php echo('5'); ?></option>
        <option value="10"<?php Owl_Html::selected( $instance['number'], '10' ); ?>><?php echo('10'); ?></option>
    </select>
</p>
<p>
    <label for="<?php echo $this->get_field_id('course'); ?>"><?php echo __('Course'); ?></label>
    <select name="<?php echo $this->get_field_name('course'); ?>" id="<?php echo $this->get_field_id('course'); ?>" class="widefat">
        <option value="0" <?php Owl_Html::selected( $instance['course'], '0' ); ?>><?php echo __('All courses'); ?></option>
        <?php
        $owl_course = new Owl_Course();
        $all_courses = $owl_course->get_all_courses();
        foreach($all_courses as $course) {
            if (is_array($course["documents"])) {
?>
        <option value="<?php echo $course["nice_url"]; ?>" <?php Owl_Html::selected( $instance['course'], $course["nice_url"] ); ?>><?php echo $course["name"]; ?></option>
<?php
            }
        }
        ?>
    </select>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Document_Widgets);

class Owl_Pages_Widgets extends Owl_Widgets {
    function Owl_Pages_Widgets(){
        $this->Owl_Widgets('pages', __('Pages'), __('Display your pages'));
    }

    function widget($instance){
        $number = !empty($instance['number']) ? $instance['number'] : 5;
        $owl_pages = new Owl_Page();
        $pages = $owl_pages->get_all_pages();
        if (!empty($pages)) {
            $pages = array_slice($pages, 0, $number);
            $to_print = array();
            foreach ($pages as $page) {
                $to_print[] = Owl_Html::make_it_link(Owl_Link::page_url($page["nice_url"], false), Owl_Format::esc_html($page["title"]));
            }
            echo Owl_Html::make_it_list($to_print);
        }
        else {
            echo __("No information");
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        $instance['number'] = $new_instance['number'];
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Pages'), 'number' => 5 ));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
<p>
    <label for="<?php echo $this->get_field_id('number'); ?>"><?php echo __('Number'); ?></label>
    <select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat">
        <option value="5" <?php Owl_Html::selected( $instance['number'], '5' ); ?>><?php echo('5'); ?></option>
        <option value="10" <?php Owl_Html::selected( $instance['number'], '10' ); ?>><?php echo('10'); ?></option>
    </select>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Pages_Widgets);

class Owl_Product_Widgets extends Owl_Widgets {
    function Owl_Product_Widgets(){
        $this->Owl_Widgets('product', __('Products'), __('Display your products'));
    }

    function widget($instance){
        $number = !empty($instance['number']) ? $instance['number'] : 5;
        $type = !empty($instance['type']) ? $instance['type'] : 'paper';
        $owl_products = new Owl_Product();
        echo $owl_products->get_product_for_widget($type, $number);
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        $instance['number'] = $new_instance['number'];
        $instance['type'] = $new_instance['type'];
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Papers'), 'number' => 5, 'type' => 'paper' ));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
<p>
    <label for="<?php echo $this->get_field_id('number'); ?>"><?php echo __('Number'); ?></label>
    <select name="<?php echo $this->get_field_name('number'); ?>" id="<?php echo $this->get_field_id('number'); ?>" class="widefat">
        <option value="5" <?php Owl_Html::selected( $instance['number'], '5' ); ?>><?php echo('5'); ?></option>
        <option value="10" <?php Owl_Html::selected( $instance['number'], '10' ); ?>><?php echo('10'); ?></option>
    </select>
</p>
<p>
    <label for="<?php echo $this->get_field_id('type'); ?>"><?php echo __('Type'); ?></label>
    <select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
        <option value="paper" <?php Owl_Html::selected( $instance['type'], 'paper' ); ?>><?php echo __('Papers'); ?></option>
        <option value="project" <?php Owl_Html::selected( $instance['type'], 'project' ); ?>><?php echo __('Projects'); ?></option>
    </select>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Product_Widgets);

class Owl_Link_Widgets extends Owl_Widgets {

    function Owl_Link_Widgets() {
        $this->Owl_Widgets('link', __('Links'), __('Display some links'));
    }

    function widget($instance) {
        Owl_Default_Content::default_links();
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Links')));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Link_Widgets);

class Owl_Language_Widgets extends Owl_Widgets {

    function Owl_Language_Widgets() {
        enqueue_style('default-lang');
        enqueue_script('default-lang');
        $this->Owl_Widgets('language', __('Language'), __('Choose the language of your site'));
    }

    function widget($instance) {
        $languages = Owl_Functions::get_available_languages();
        $to_print = array();
        foreach ($languages as $lang_code => $lang_name) {
            $to_print[] = '<a href="#" rel="' . $lang_code .'" class="change-lang ' . $lang_code . '"><span>' . $lang_name . '</span></a>';
        }
        echo Owl_Html::make_it_list($to_print);
        echo '<script type="text/javascript">';
        echo "/* <![CDATA[ */\n";
        echo '$(".change-lang").click(function(){
                $.changeLang(this);
                return false;
            });';
        echo "/* ]]> */\n";
        echo '</script>';
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = Owl_Format::esc_html(strip_tags($new_instance['title']));
        return $instance;
    }

    function form( $instance ) {
        $instance = Owl_System_Helper::array_merge((array) $instance, array( 'title' => __('Language')));
    ?>
<p>
    <label for="<?php echo $this->get_field_id('title'); ?>" >
        <?php echo __('Title') . ":"; ?>
    </label>
    <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance["title"]; ?>" class="widefat"/>
</p>
    <?php
    }
}

//Register it
register_widget(Owl_Language_Widgets);
